/*******************************************************************************
 * This file is part of blueprint4j www.blueprint4j.org.
 * 
 *     blueprint4j is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Lesser General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     blueprint4j is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU Lesser General Public License for more details.
 * 
 *     You should have received a copy of the GNU Lesser General Public License
 *     along with blueprint4j. If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package blueprint4j.gui;

import java.awt.*;
import java.io.*;
import javax.swing.*;
import javax.swing.border.*;

import blueprint4j.Constants;

public class DataGroup extends JPanel {
    private TitledBorder border = BorderFactory.createTitledBorder("");//BorderFactory.createLineBorder(Color.BLACK));
    private GridBagLayout bag=new GridBagLayout();
    private GridBagConstraints constraints=new GridBagConstraints(0,0, 1, 1,1,1,GridBagConstraints.EAST,GridBagConstraints.NONE, new Insets(1,4,2,4),0,0);
    private JPanel center=new JPanel(bag);
    private JPanel label=null;
    private String name="";
    
    public DataGroup() {
        super(new BorderLayout());
        try {
            build(null,null,0);
        } catch (IOException ioe) {}
    }
    
    public DataGroup(String name) {
        super(new BorderLayout());
        try {
            build(name, null, 0);
        } catch (IOException ioe) {}
    }
    
    public DataGroup(String name, String icon_resource_name)
    throws IOException {
        super(new BorderLayout());
        build(name, new String[]{icon_resource_name}, 1);
    }
    
    public DataGroup(String name, String[] icon_resource_names, int fps)
    throws IOException {
        super(new BorderLayout());
        build(name,icon_resource_names,fps);
    }
    
    private void build(String p_name, String[] icon_resource_names, int fps)
    throws IOException {
        name=p_name;
        Gui.addEnter4FocusShift(this);
        // Set title
        if (name != null) {
            border.setTitle(name);
        }
        setBorder(border);
        if (icon_resource_names != null) {
            // Load and add Icon
            label=new JPanel(new FlowLayout());
            label.add(new AnimatingLabel(icon_resource_names, fps));
            add(label, BorderLayout.WEST);
        }
        JPanel panel=new JPanel(new BorderLayout());
        panel.add(center,BorderLayout.WEST);
        add(panel,BorderLayout.CENTER);
    }
    
    public void add(JComponent single){
        constraints.gridx=0;
        constraints.gridwidth=2;
        constraints.fill=GridBagConstraints.NONE;
        constraints.anchor=GridBagConstraints.WEST;
        constraints.weighty=0;
        bag.setConstraints(single,constraints);
        center.add(single);
        constraints.gridy++;
    }
    
    public void addTail(){
        constraints.gridx=0;
        constraints.gridwidth=2;
        constraints.fill=GridBagConstraints.BOTH;
        constraints.anchor=GridBagConstraints.WEST;
        constraints.weighty=10000;
        JComponent c=new JLabel();
        bag.setConstraints(c,constraints);
        center.add(c);
        constraints.gridy++;
    }

    public void add(DataPair pair){
        constraints.gridx=0;
        constraints.gridwidth=1;
        constraints.fill=GridBagConstraints.NONE;
        constraints.anchor=GridBagConstraints.EAST;
        constraints.weighty=0;
        bag.setConstraints(pair.getLabel(),constraints);
        center.add(pair.getLabel());
        constraints.gridx=1;
        constraints.anchor=GridBagConstraints.WEST;
        bag.setConstraints(pair.getComponent(),constraints);
        center.add(pair.getComponent());
        constraints.gridy++;
    }
    
    public void setBackground(Color color){
        super.setBackground(color);
        if (label!=null) label.setBackground(color);
        if (center!=null) {
            center.setBackground(color);
        }
    }
    
    public String getName(){
        return name;
    }
}

